
package com.shuyou;

import com.shuyou.fragments.AllLabelFragment;
import com.shuyou.fragments.HotLabelFragment;
import com.shuyou.widget.SlidePagerView;
import com.shuyou.widget.SlidePagerView.OnSlidePagerSelectedListener;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

/**
 * 搜索页面
 * @author jiabin
 *
 */
public class SearchActivity extends FragmentActivity implements OnClickListener , OnSlidePagerSelectedListener {
    private EditText searchEditText;

    private ImageView searchButton;

    private Animation shake;

    private ArrayList<Fragment> viewPagers;

    private HotLabelFragment hotLabelFragment;

    private AllLabelFragment allLabelFragment;

    private SlidePagerView slidePagerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_search);
        LinearLayout tabsLayout = (LinearLayout) findViewById(R.id.search_label_tab_layout);
        shake = AnimationUtils.loadAnimation(SearchActivity.this, R.anim.shake);
        searchEditText = (EditText) findViewById(R.id.search_edit);
        searchButton = (ImageView) findViewById(R.id.search_image_searchbutton);
        searchButton.setOnClickListener(this);
        findViewById(R.id.search_back).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }

        });

        hotLabelFragment = new HotLabelFragment();
        allLabelFragment = new AllLabelFragment();
        ArrayList<String> tabNames = new ArrayList<String>();
        tabNames.add("热门");
        tabNames.add("分类");
        viewPagers = new ArrayList<Fragment>();
        viewPagers.add(hotLabelFragment);
        viewPagers.add(allLabelFragment);
        slidePagerView = new SlidePagerView(this, getSupportFragmentManager(), tabNames,
                viewPagers, 0, tabNames.size(), 0);
        slidePagerView.setOnSlidePagerSelectedListener(this);
        View localView = slidePagerView.getSlidePagerView();
        tabsLayout.addView(localView);

        /*
         * TableLayout labelTable =
         * (TableLayout)findViewById(R.id.search_label_table); int numRow = 4;
         * for(int i = 0; i < numRow; i++) { LinearLayout rowContent =
         * (LinearLayout
         * )getLayoutInflater().inflate(R.layout.shuyou_table_label, null);
         * rowContent
         * .findViewById(R.id.shuyou_table_label1).setOnClickListener(new
         * OnClickListener(){
         * @Override public void onClick(View arg0) { // TODO Auto-generated
         * method stub } });
         * rowContent.findViewById(R.id.shuyou_table_label2).setOnClickListener
         * (new OnClickListener(){
         * @Override public void onClick(View arg0) { // TODO Auto-generated
         * method stub } }); TableRow tableRow = new TableRow(this);
         * tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
         * LayoutParams.WRAP_CONTENT)); tableRow.addView(rowContent);
         * labelTable.addView(tableRow, i); }
         */
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_image_searchbutton:
                String content = searchEditText.getText().toString();
                if (content.trim().equals("")) {
                    searchButton.startAnimation(shake);
                    searchEditText.startAnimation(shake);
                } else {
                    Intent i = new Intent(SearchActivity.this, SearchResultActivity.class);
                    i.putExtra("keyword", content);
                    startActivity(i);
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onSlidePagerSelected(int index, Fragment selectedView) {
        // TODO Auto-generated method stub
        
    }
}
